Jerry's Blogs

Thursday, November 29, 2007

On Bad Code Day

It's like a bad hair day, except at the end of a bad code day, you usually don't have much hair left.

I should have noticed all the glaring warning signs before I started. Sunny skies, fantastic Y-Combinator infosession, solid meals... It was too good to last. Starting from worst news, I felt the full brunt of Rails 'Black Magic' working against me. I read through all the code and docs regarding ActiveRecord validation Callbacks only to find that most methods have empty bodies (magic!). I wanted our user model to be tested, so I tried overriding validations when isguest is true. It sounded simple enough. On top of that there were many many things that looked like what I wanted to do in the examples of the docs. I first tried to override 'save' to return true if guest and call super otherwise. Well for starters, it took me hours to realize I was using 'super' wrong by doing super.save instead of just plain super. To make matters worse, after I fixed my syntax error, only half of what I wanted would work. Namely, just manipulating users did what I expected, but adding a guest user to a 'gathering' would invalidate both the 'user' and the 'gathering' and leave them both unsaved. I tried to override and test various things in the 'gathering' model, but that yielded nothing either.

At this point, I got so sick of testing by hand that I thought I'd do the right thing and fill in some unit tests. Right away that we had no tests and that our fixtures were out of date. This meant spending time just updating the fixtures before I could even _approach_ testing. Only after futzing the tests did I notice a glaring mistake. I noticed that even freshly loaded fixtures would not validate. Impossible! Sadly, it was true. The culprit turned out to be two illogical validations: validates_presence_of :password, :password_confirmation. These two attributes are virtual fields used by validates_confirmation_of :password, but there are _no_ columns in the actual table for these two fields. What's actually stored is hashed_password, so it makes sense to validates_presence_of :hashed_password.

You'd think I'd reach at happy ending after all these hours, but on a bad code day , you simply cannot win. :(

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home